<template>
  <div class="index-page">
    <div class="sec-bg"
         :style="{background: 'url(http://47.116.131.249:8080/group1/M00/00/00/rBooP1-IWuuAeeDIAAbhM9Ra0cE042.png) no-repeat center center', backgroundSize: 'contain'}"></div>
    <Back-top></Back-top>
    <Nav active-name="home"/>

    <div class="sec-01 flex-row-v-center">
      <div class="article-holder flex-1">
        <div class="carousel-box">
          <Carousel
            v-if="carouselList.length > 0"
            :autoplay="setting.autoplay"
            :autoplay-speed="setting.autoplaySpeed"
            :dots="setting.dots"
            :arrow="setting.arrow">
            <Carousel-item v-for="item in carouselList" :key="item.id">
              <img class="carousel-item flex-center" :src="item.img" alt=""/>
            </Carousel-item>
          </Carousel>
        </div>
      </div>
      <div class="aside-holder">
        <div class="deal-box flex-col-v-center">
          <div class="title flex-center">成交看板</div>
          <div class="stat-box flex-col-v-center">
            <div class="name">总交易量</div>
            <div class="num">{{ stat.totalDeal }}</div>
          </div>
          <div class="item-group flex-row-v-center">
            <div class="num-box flex-1 flex-col-v-center">
              <div class="name">在售账号</div>
              <div class="num">{{ stat.totalAccount }}</div>
            </div>
            <div class="line"></div>
            <div class="num-box flex-1 flex-col-v-center">
              <div class="name">今日访问</div>
              <div class="num">{{ stat.todayVisit }}</div>
            </div>
          </div>
          <div class="btn flex-row-center" @click="goToPayPage">
            <img src="~assets/img/icon-cart.png"/>
            我要卖号
          </div>
        </div>
      </div>
    </div>

    <div class="sec-02">
      <div class="header-holder flex-row-space-between">
        <div class="content">
          <span class="label" :class="{'active': recommendIdx === 'djzh'}" @click="selectRecommend('djzh')">顶级账号</span>
          <span class="label" :class="{'active': recommendIdx === 'zdqt'}" @click="selectRecommend('zdqt')">置顶强推</span>
          <span class="label" :class="{'active': recommendIdx === 'zxzh'}" @click="selectRecommend('zxzh')">最新账号</span>
          <span class="label" :class="{'active': recommendIdx === 'zrsx'}" @click="selectRecommend('zrsx')">昨日上新</span>
        </div>
        <div class="more flex-row-v-center">查看全部 <img src="~assets/img/icon-right-arrow.png"/></div>
      </div>
      <div class="content-holder" v-if="accountList.length > 0">
        <div class="flex-row-v-center" v-for="list in accountList">
          <div class="card-box" v-for="item in list.val" @click="goToAccountInfoPage(item.id)">
            <div class="img-box" v-if="item.id">
              <img :src="item.img" :alt="item.title"/>
              <div class="hot-box flex-row-v-center">
                <img src="~assets/img/icon-hot.png">
                {{ item.hot }}
              </div>
            </div>
            <div class="content-box" v-if="item.id">
              <div class="name-txt">
                {{ item.title }}
              </div>
              <div class="line"></div>
              <div class="price-time-box flex-row-space-between">
                <div class="price-box flex-row-v-center">
                  <div class="unit">¥</div>
                  {{ item.price | moneyFormat }}
                </div>
                <div class="time-box flex-row-v-center">
                  <img src="~assets/img/icon-time.png">{{ item.launch_time }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="content-holder" v-else>
        <div class="empty-box flex-center">
          暂无记录~
        </div>
      </div>
    </div>

    <div class="sec-03">
      <div class="header-holder flex-row-space-between">
        <div class="title">每日成交</div>
        <div class="more flex-row-v-center">查看全部 <img src="~assets/img/icon-right-arrow.png"/></div>
      </div>
      <div class="content-holder">
        <div class="flex-row-v-center" v-if="topStat.count && dateStat.length > 0">
          <div class="card-box flex-col-v-center">
            <div class="img-box">
              <div class="num">{{ topStat.count }}/单</div>
              <img :src="topStat.img"/>
            </div>
            <div class="txt flex-center">顶级账号成交记录</div>
          </div>
          <div class="card-box flex-col-v-center" v-for="(item, idx) in dateStat" :key="idx">
            <div class="img-box">
              <div class="num">{{ item.count }}/单</div>
              <img :src="item.img" alt=""/>
            </div>
            <div class="txt flex-center">{{ item.date }}已出售</div>
          </div>
        </div>
        <div class="empty-box flex-center" v-else>
          暂无成交记录~
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Core from '@/core/core'

export default {
  data() {
    return {
      setting: {
        autoplay: true,
        autoplaySpeed: 5000,
        dots: 'inside',
        arrow: 'always'
      },
      carouselList: [],
      recommendIdx: 'djzh',
      accountList: [],
      topStat: {},
      dateStat: [],
      stat: {
        totalDeal: 0,
        totalAccount: 0,
        todayVisit: 0
      }
    }
  },
  mounted() {
    this.$jsonp('https://apis.map.qq.com/ws/location/v1/ip?key=5OLBZ-CM5CD-OL34L-HTXIK-YNOQE-LPFGB&output=jsonp')
      .then(res => {
        let ip = res.result.ip
        return Core.Api.Home.track({ip: ip})
      })
      .then(() => {
        console.log('埋点成功')
      })
      .catch(err => {
        console.log(err)
      })
    this.getHomeStat()
    this.getCarouselList()
    this.getAccountList(1, 0)
    this.getGameStat()
  },
  methods: {
    getHomeStat() {
      this._getHomeStat(res => {
        this.stat.totalDeal = res.totalDealOnSale
        this.stat.totalAccount = res.totalAccountOnSale
        this.stat.todayVisit = res.todayVisit
      })
    },
    getCarouselList() {
      this._getAdList(3, list => {
        this.carouselList = list
      })
    },
    selectRecommend(type) {
      if (this.recommendIdx === type) {
        return;
      }
      this.recommendIdx = type
      if (this.recommendIdx === 'djzh') {
        this.getAccountList(1, 0)
      } else if (this.recommendIdx === 'zdqt') {
        this.getAccountList(0, 1)
      } else if (this.recommendIdx === 'zxzh') {
        this.getAccountLatestList(0)
      } else if (this.recommendIdx === 'zrsx') {
        this.getAccountLatestList(1)
      }
    },
    getAccountList(topLevel, up) {
      this._getHomeList(topLevel, up, 12, list => {
        let result = []
        if (list.length > 0 && list.length % 4 > 0) {
          let num = 4 - (list.length % 4);
          for (let i = 0; i < num; i++) {
            list.push({})
          }
        }
        for (let i = 0; i < list.length / 4; i++) {
          result.push({
            key: i,
            val: list.slice(4 * i, 4 * i + 4)
          })
        }
        this.accountList = result
      })
    },
    getAccountLatestList(yesterday) {
      this._getHomeLatestList(yesterday, 12, list => {
        let result = []
        if (list.length > 0 && list.length % 4 > 0) {
          let num = 4 - (list.length % 4);
          for (let i = 0; i < num; i++) {
            list.push({})
          }
        }
        for (let i = 0; i < list.length / 4; i++) {
          result.push({
            key: i,
            val: list.slice(4 * i, 4 * i + 4)
          })
        }
        this.accountList = result
      })
    },
    getGameStat() {
      Core.Api.GameAccount.topLevelDeal({})
        .then(res => {
          this.topStat = res
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
      Core.Api.GameAccount.statDeal({})
        .then(res => {
          this.dateStat = res.list
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    goToPayPage() {
      this.$router.push("/found-pay")
    },
    goToAccountInfoPage(id) {
      window.open("/account-on-sale/detail?id=" + id, '_blank')
    },
    _getHomeStat(cb) {
      Core.Api.Home.stat({})
        .then(res => {
          if (cb) {
            cb(res)
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    _getAdList(type, cb) {
      Core.Api.Ad.list({type: type})
        .then(res => {
          if (cb) {
            cb(res.list)
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    _getHomeList(topLevel, up, limit, cb) {
      Core.Api.GameAccount.homeList({topLevel: topLevel, up: up, limit: limit})
        .then(res => {
          if (cb) {
            cb(res.list)
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    _getHomeLatestList(yesterday, limit, cb) {
      Core.Api.GameAccount.homeLatestList({yesterday: yesterday, limit: limit})
        .then(res => {
          if (cb) {
            cb(res.list)
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
  }
}
</script>

<style lang="scss" scoped>
.index-page {
  background: rgb(245, 245, 245);
  position: relative;

  .sec-bg {
    width: 100%;
    height: 934px;
    position: absolute;
  }

  .sec-01 {
    padding-top: 20px;
    width: 1200px;
    margin: auto;
    position: relative;
    z-index: 2;

    .article-holder {
      margin-right: 20px;

      .carousel-box {
        width: 850px;
        height: 350px;

        .carousel-item {
          width: 100%;
          height: 350px;
        }
      }
    }

    .aside-holder {
      width: 330px;

      .deal-box {
        height: 350px;
        background: #FFFFFF;
        border-radius: 6px;
        padding: 0 20px 30px 20px;

        .title {
          width: 154px;
          height: 40px;
          background: #F5F5F5;
          font-weight: 600;
          font-size: 18px;
          color: #F58B01;
          background: url("~assets/img/banner-bg.png") center center;
          background-size: cover;
        }

        .stat-box {
          margin-top: 28px;

          .name {
            font-size: 14px;
            color: #3A3D41;
            line-height: 20px;
          }

          .num {
            font-weight: 500;
            font-size: 30px;
            color: #F58B01;
            margin-top: 2px;
            line-height: 42px;
          }
        }

        .item-group {
          width: 100%;
          margin-top: 40px;

          .num-box {
            .name {
              font-size: 14px;
              color: #3A3D41;
              line-height: 20px;
            }

            .num {
              font-weight: 500;
              font-size: 24px;
              color: #3A3D41;
              line-height: 33px;
            }
          }

          .line {
            width: 1px;
            height: 30px;
            background: rgb(245, 245, 245);
          }
        }

        .btn {
          background-image: linear-gradient(270deg, #FBBF02 0%, #F58A01 100%);
          border-radius: 4px;
          font-weight: 500;
          font-size: 18px;
          color: #FFFFFF;
          text-align: center;
          width: 100%;
          height: 60px;
          margin-top: 33px;
          cursor: pointer;

          img {
            width: 20px;
            height: 20px;
            margin-right: 5px;
          }
        }
      }
    }
  }

  .sec-02 {
    position: relative;
    padding-top: 20px;
    width: 1200px;
    margin: auto;
    z-index: 2;

    .header-holder {
      width: 100%;
      height: 74px;
      background: #FFFFFF;
      border-radius: 6px;
      padding: 20px;

      .content {
        font-size: 0;

        .label {
          margin-right: 14px;
          display: inline-block;
          background: #F5F5F5;
          border-radius: 17px;
          font-size: 14px;
          color: #7F8389;
          padding: 7px 16px;
          line-height: 20px;
          cursor: pointer;

          &.active {
            background: #F58B01;
            color: #FFFFFF;
          }

          &:last-child {
            margin-right: 0;
          }
        }
      }
    }

    .content-holder {
      width: 100%;

      .card-box {
        margin-top: 20px;
        margin-right: 20px;
        flex: 1;
        background: #FFFFFF;
        border-radius: 6px;
        font-size: 0;
        overflow: hidden;
        cursor: pointer;

        &:last-child {
          margin-right: 0;
        }

        &:hover {
          .img-box img {
            transform: scale(1.2);
          }
        }

        .img-box {
          position: relative;
          height: 200px;
          overflow: hidden;
          font-size: 0;

          img {
            width: 100%;
            height: 200px;
            transition: all 0.6s;
          }

          .hot-box {
            background: rgba(0, 0, 0, .6);
            border-radius: 20px 0 0 20px;
            font-size: 14px;
            color: #FFFFFF;
            position: absolute;
            top: 20px;
            right: 0;
            padding: 0 12px;
            height: 30px;

            img {
              width: 14px;
              height: 14px;
              margin-right: 2px;
            }
          }
        }

        .content-box {
          padding: 0 14px;

          .name-txt {
            margin: 14px 0;
            font-size: 14px;
            color: #3A3D41;
            line-height: 20px;
            height: 60px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            line-clamp: 3;
            -webkit-box-orient: vertical;
          }

          .line {
            width: 100%;
            height: 0;
            border-top: 1px dashed #ECECEC;
          }

          .price-time-box {
            height: 40px;

            .price-box {
              font-weight: 500;
              font-size: 20px;
              color: #F0562D;
              line-height: 28px;

              .unit {
                font-size: 13px;
                line-height: 18px;
                margin-right: 4px;
                align-self: flex-end;
                position: relative;
                bottom: 3px;
              }
            }

            .time-box {
              font-size: 12px;
              color: #7F8389;

              img {
                width: 14px;
                height: 14px;
                margin-right: 4px;
              }
            }
          }
        }
      }
    }
  }

  .sec-03 {
    position: relative;
    padding: 20px 0 6px 0;
    width: 1200px;
    margin: auto;
    z-index: 2;

    .header-holder {
      width: 100%;
      height: 68px;
      background: #FFFFFF;
      border-radius: 6px;
      padding: 20px;

      .title {
        font-weight: 600;
        font-size: 20px;
        color: #3A3D41;
        line-height: 28px;
      }
    }

    .content-holder {
      width: 100%;

      .card-box {
        margin-top: 20px;
        background: #FFFFFF;
        margin-right: 20px;
        flex: 1;
        border-radius: 6px;
        overflow: hidden;
        cursor: pointer;

        &:last-child {
          margin-right: 0;
        }

        &:hover {
          .txt {
            color: #F58B01;
          }
        }

        .img-box {
          height: 200px;
          font-size: 0;
          position: relative;

          img {
            width: 100%;
            height: 100%;
          }

          .num {
            position: absolute;
            left: 10px;
            top: 10px;
            padding: 0 8px;
            line-height: 32px;
            border-radius: 5px;
            z-index: 10;
            background: rgba(0, 0, 0, 0.5);
            font-size: 14px;
            color: #ffffff;
          }
        }

        .txt {
          width: 100%;
          height: 54px;
          padding: 10px;
        }
      }
    }
  }

  .empty-box {
    position: relative;
    background: #FFFFFF;
    margin-top: 20px;
    padding: 20px;
    z-index: 2;
  }

  .more {
    position: relative;
    font-size: 14px;
    color: #A6ACB3;
    cursor: pointer;
    z-index: 2;

    img {
      width: 14px;
      height: 14px;
      margin-left: 2px;
    }
  }
}
</style>
